package playchess;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Index;
import com.google.appengine.api.datastore.Index.IndexState;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyRange;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.TransactionOptions;

public class DatastoreWithTransaction 
		implements DatastoreService
{
	private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	public DatastoreWithTransaction()
	{
		;
	}
	
	//-------------------------------------------------------------------//
	@Override
	public Collection<Transaction> getActiveTransactions() 
	{
		return datastore.getActiveTransactions();
	}

	@Override
	public Transaction getCurrentTransaction() 
	{
		return datastore.getCurrentTransaction();
	}

	@Override
	public Transaction getCurrentTransaction(Transaction returnedIfNoTxn) 
	{
		return datastore.getCurrentTransaction(returnedIfNoTxn);
	}

	@Override
	public PreparedQuery prepare(Query query)
	{
		return datastore.prepare(query);
	}

	@Override
	public PreparedQuery prepare(Transaction txn, Query query)
	{
		return datastore.prepare(txn, query);
	}

	@Override
	public KeyRangeState allocateIdRange(KeyRange range) 
	{
		return datastore.allocateIdRange(range);
	}

	@Override
	public KeyRange allocateIds(String kind, long num) 
	{
		return datastore.allocateIds(kind, num);
	}

	@Override
	public KeyRange allocateIds(Key parent, String kind, long num) 
	{
		return datastore.allocateIds(parent, kind, num);
	}

	@Override
	public Transaction beginTransaction()
	{
		return datastore.beginTransaction();
	}

	@Override
	public Transaction beginTransaction(TransactionOptions options)
	{
		return datastore.beginTransaction(options);
	}

	@Override
	public void delete(Key... keys)
	{
		Transaction txn = datastore.beginTransaction();
		
		datastore.delete(keys);
		
		try
		{
			txn.commit();
		}
		catch(Exception e)
		{
			System.out.println("Something went wrong: " + e.getMessage() );
		}
	}

	@Override
	public void delete(Iterable<Key> keys)
	{
		datastore.delete(keys);
	}

	@Override
	public void delete(Transaction txn, Key... keys)
	{
		datastore.delete(txn, keys);
	}

	@Override
	public void delete(Transaction txn, Iterable<Key> keys)
	{
		datastore.delete(txn, keys);
	}

	@Override
	public Entity get(Key key) 
			throws EntityNotFoundException
	{
		return datastore.get(key);
	}

	@Override
	public Map<Key, Entity> get(Iterable<Key> keys) 
	{
		return datastore.get(keys);
	}

	@Override
	public Entity get(Transaction txn, Key key) 
			throws EntityNotFoundException
	{
		return datastore.get(txn, key);
	}

	@Override
	public Map<Key, Entity> get(Transaction txn, Iterable<Key> keys) 
	{
		return datastore.get(txn, keys);
	}

	@Override
	public DatastoreAttributes getDatastoreAttributes() 
	{
		return datastore.getDatastoreAttributes();
	}

	@Override
	public Map<Index, IndexState> getIndexes() 
	{
		return datastore.getIndexes();
	}

	@Override
	public Key put(Entity entity) 
	{
		Transaction txn = datastore.beginTransaction();
		
		Key key = datastore.put(entity);
		
		try
		{
			txn.commit();
		}
		catch(Exception e)
		{
			System.out.println("Something went wrong: " + e.getMessage() );
		}
		return key;
	}

	@Override
	public List<Key> put(Iterable<Entity> entities) 
	{
		return datastore.put(entities);
	}

	@Override
	public Key put(Transaction txn, Entity entity) 
	{
		return put(txn, entity);
	}

	@Override
	public List<Key> put(Transaction txn, Iterable<Entity> entities) 
	{
		return datastore.put(txn, entities);
	}
}
